﻿//战斗Layer，实现了自由点击移动功能，六边形地形贴图功能

var mainMap = {
	node : null,
	
	handlingMoving : null,
	
	nodeBBox : null,
	
	LayerToNodeCoord : function(coord){
		return {x : coord.x - this.node.x, y : coord.y - this.node.y};
	},
	
	NodeToLayerCoord : function(coord){
		return {x : coord.x + this.node.x, y : coord.y + this.node.y};
	}
};

//terrain tiles
var terrain = {
	//terrain
	terrainblankSprites : null,
	terrainSprites : null,
	tileSprites : null,
	forceflagSprite : null,
};

//handler as: function OnClicked(touch, event){}   param as: cocos2d-js onTouchEnded:function (touch, event) {}
var onBattleLayerClicked = [];
var battleLayerClickEnable = true;

var BattleLayer = cc.Layer.extend({
	ctor: function () {
		this._super();

		cc.spriteFrameCache.addSpriteFrames(resource.tiles_grass_plist);
		cc.spriteFrameCache.addSpriteFrames(resource.tiles_forest_plist);
		cc.spriteFrameCache.addSpriteFrames(resource.tiles_urban_plist);
		cc.spriteFrameCache.addSpriteFrames(resource.tiles_river_plist);
		cc.spriteFrameCache.addSpriteFrames(resource.tiles_water_plist);
		
		cc.spriteFrameCache.addSpriteFrames(resource.flags_plist);
		
		this._AddFreeMoveMap();
		this._AddTileAndTerrain();
		this._AddTouchListener();
	},
	
	_AddFreeMoveMap: function(){
		mainMap.node = new cc.Node();
		this.addChild(mainMap.node, 0);
		
		//set mainNode to center
		var winSize = cc.director.getWinSize();
		coordStart = tileEngine.IndexToCoord(0);
		coordEnd = tileEngine.IndexToCoord(tileEngine.tileCount - 1);
		coordCenter = {x : 0.5 * (coordStart.x + coordEnd.x), y : 0.5 * (coordStart.y + coordEnd.y)};
		coordDiv = {x : 0.5 * winSize.width - coordCenter.x, y : 0.5 * winSize.height - coordCenter.y};
		mainMap.node.attr(coordDiv);
	},
	
	_AddTileAndTerrain: function(){
		
		mainMap.nodeBBox = {
			left: - tileEngine.width - 100 + campaignScene.battlelayerBBox.right,
			bottom: - tileEngine.height - 100 + campaignScene.battlelayerBBox.top,
			right: 100,
			top: 100
		};
		
		terrain.terrainblankSprites = new Array(tileEngine.tileCount);
		terrain.terrainSprites = new Array(tileEngine.tileCount);
		terrain.tileSprites = new Array(tileEngine.tileCount);
		
		for (var index = 0; index < tileEngine.tileCount; index++) {
			var coord = tileEngine.IndexToCoord(index);

			this.BuildBlankTerrainTile(index, coord);
			this.BuildTerrainTile(index, coord);				
			terrain.tileSprites[index] = new cc.Sprite(campaign.zone.base_image);
			terrain.tileSprites[index].attr( coord );
			mainMap.node.addChild(terrain.tileSprites[index],2);
		}
		
		terrain.forceflagSprite = new Array(campaign.force.length);
		for (var forceid = 0; forceid < campaign.force.length; forceid++){
			var flag = new cc.Sprite(resource.flag_png);
			var coord = tileEngine.IndexToCoord(campaign.force[forceid].tileIndex);
			var colorarry = campaign.force[forceid].color;
			var color = cc.color(colorarry[0],colorarry[1],colorarry[2]);
			flag.attr({x:coord.x, y:coord.y, scale:0.5, color: color});
			var aura = CreateAuraSprite(color);
			aura.attr({x:0.5*flag.width, y:0.5*flag.height, scale:1/flag.scale});
			flag.addChild(aura, -1);
			mainMap.node.addChild(flag, 3);
			terrain.forceflagSprite[forceid] = flag;
		}
	},
	
	_AddTouchListener: function(){
		var tilelistener = cc.EventListener.create({
			event: cc.EventListener.TOUCH_ONE_BY_ONE, //TOUCH_ONE_BY_ONE 为单次触摸事件监听器
			swallowTouches: false,
			onTouchBegan:function (touch,event) {
				var winloc = touch.getLocation();
				if (winloc.x < campaignScene.battlelayerBBox.left
				|| winloc.x > campaignScene.battlelayerBBox.right
				|| winloc.y < campaignScene.battlelayerBBox.bottom
				|| winloc.y > campaignScene.battlelayerBBox.top
				)
					return false;
				
				return true;
			},
			onTouchMoved: function (touch, event) {
				var delta = touch.getDelta();
				if (Math.abs(delta.x) + Math.abs(delta.y) > 2){
					mainMap.handlingMoving = true;
					mainMap.node.x += delta.x;
					mainMap.node.y += delta.y;
				}
			},
			onTouchEnded:function (touch, event) {
				if (mainMap.handlingMoving){
					mainMap.handlingMoving = false;
					//view goback
					var backX = mainMap.node.x;
					var backY = mainMap.node.y;
					var bMove = false;
					if (backX < mainMap.nodeBBox.left) {
						backX = mainMap.nodeBBox.left; bMove = true;
					}else if (backX > mainMap.nodeBBox.right) {
						backX = mainMap.nodeBBox.right; bMove = true;
					}
					if (backY < mainMap.nodeBBox.bottom) {
						backY = mainMap.nodeBBox.bottom; bMove = true;
					}else if (backY > mainMap.nodeBBox.top) {
						backY = mainMap.nodeBBox.top; bMove = true;
					}
					if (bMove){
						mainMap.node.runAction(cc.moveTo(0.2, backX, backY));
					}
				}else{
					if (battleLayerClickEnable){
						for (var idx in onBattleLayerClicked){
							onBattleLayerClicked[idx](touch, event);
						}
					}
				}
			}
		});

		cc.eventManager.addListener(tilelistener, this);
	},

	BuildBlankTerrainTile : function(index, coord){
		terrainIndexEngine.SetAllheadingTag();
		if (terrain.terrainblankSprites[index] != null)
			terrain.terrainblankSprites[index].removeFromParent();
		var landtype = campaign.zone.land_type_default;
		terrain.terrainblankSprites[index] = new cc.Sprite(tilesMapping.land_type_image[landtype].head
				+ terrainIndexEngine.GetIndexString() + ".png");

		terrain.terrainblankSprites[index].attr( coord );
		mainMap.node.addChild(terrain.terrainblankSprites[index],0);
	},

	BuildTerrainTile : function(index, coord){
		terrainIndexEngine.ClearTag();
		var landtype = campaign.zone.land_type[index];
		for (var neighborid in tileEngine.neighborCache[index]){
			var tgtindex = tileEngine.neighborCache[index][neighborid];
			if (landtype == campaign.zone.land_type[tgtindex]){
				var heading = tileEngine.tileNet.TwoTileHeading(tileEngine.IndexToTile(index), tileEngine.IndexToTile(tgtindex));
				var tag = tileEngine.tileNet.HeadingToNeighbor(heading);
				terrainIndexEngine.SetTerrainTag(tag, true);
			}
		}
		if (terrain.terrainSprites[index] != null)
			terrain.terrainSprites[index].removeFromParent();		
		terrain.terrainSprites[index] = new cc.Sprite(tilesMapping.land_type_image[landtype].head
				+ terrainIndexEngine.GetIndexString() + ".png");

		terrain.terrainSprites[index].attr( coord );
		mainMap.node.addChild(terrain.terrainSprites[index],1);
	},
	
	BlinkTile : function(tileIndex){
		var blink = cc.blink(0.2, 1);
		terrain.tileSprites[tileIndex].runAction(blink);
	}
});

var CreateAuraSprite = function(color){
	var aura = new cc.Sprite(resource.units_aura_png);
	aura.setColor(color);
	aura.setOpacity(127);
	return aura;
};